package com.tongzhou.tongzhou.account.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IAccountContactDAO;
import com.tongzhou.tongzhou.account.model.AccountContact;
import com.tongzhou.tongzhou.account.model.SearchAccountContactCondition;
import com.tongzhou.util.Util;

public class AccountContactDAO implements IAccountContactDAO{

	@Override
	public int addAccountContact(AccountContact accountContact)
			throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into account_contact(name,position,telephone,email,account_id) ");
		sql.append("values('"+Util.changeToObj(accountContact.getName())+"',");
		sql.append("'"+Util.changeToObj(accountContact.getPosition())+"',");
		sql.append("'"+Util.changeToObj(accountContact.getTelephone())+"',");
		sql.append("'"+Util.changeToObj(accountContact.getEmail())+"',");
		sql.append("'"+accountContact.getAccount_id()+"');");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deleteAccountContact(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from account_contact where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public AccountContact getAccountContactById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,name,position,telephone,email,account_id ");
		sql.append(" from account_contact ");
		sql.append(" where id='"+id+"'");
		AccountContact accountContact=DBUtil.query(sql.toString(), new BeanHandler<AccountContact>(AccountContact.class));
		return accountContact;
	}

	@Override
	public List<AccountContact> listAccountContact(
			SearchAccountContactCondition condition) throws DBException {
		List<AccountContact> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from account_contact ");
		sql.append(" where 1=1 ");
		sql.append(" and account_id='"+condition.getAccount_id()+"'");
		
		//总记录数
		Map<String,String> dataMap=new HashMap<String,String>();
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,name,position,telephone,email,account_id ");
		sql.append(" from account_contact ");
		sql.append(" where 1=1 ");
		sql.append(" and account_id='"+condition.getAccount_id()+"'");
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<AccountContact>(AccountContact.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateAccountContact(AccountContact accountContact)
			throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update account_contact set ");
		sql.append("name='"+Util.changeToObj(accountContact.getName())+"',");
		sql.append("position='"+Util.changeToObj(accountContact.getPosition())+"',");
		sql.append("telephone='"+Util.changeToObj(accountContact.getTelephone())+"',");
		sql.append("email='"+Util.changeToObj(accountContact.getEmail())+"' ");
		sql.append("where id='"+accountContact.getId()+"'");
		DBUtil.update(sql.toString());
	}
	
	
}
